package com.wufei.tastefor.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.viewpagerindicator.CirclePageIndicator;
import com.wufei.tastefor.R;
import com.wufei.tastefor.adapter.HotMenuAdapter;
import com.wufei.tastefor.adapter.HotPeopleAdapter;
import com.wufei.tastefor.adapter.SpecialAdapter;
import com.wufei.tastefor.bean.BaseBean;
import com.wufei.tastefor.bean.HotMenu;
import com.wufei.tastefor.bean.HotPeople;
import com.wufei.tastefor.bean.Lab;
import com.wufei.tastefor.bean.NewMenu;
import com.wufei.tastefor.bean.NowFood;
import com.wufei.tastefor.bean.Product;
import com.wufei.tastefor.bean.Special;
import com.wufei.tastefor.bean.Work;
import com.wufei.tastefor.utils.AppConfig;
import com.wufei.tastefor.widget.GridViewForScollView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;



/**
 * A simple {@link Fragment} subclass.
 */

@EFragment(R.layout.fragment_home)
public class HomeFragment extends Fragment implements ViewPager.OnPageChangeListener {


    @ViewById
    ViewPager homeFirstViewpager;
    @ViewById
    CirclePageIndicator homeFirstIndicator;
    @ViewById
    TextView homeFirstHot;
    @ViewById
    SimpleDraweeView homeFirstUserIcon;
    @ViewById
    TextView homeFirstName;
    @ViewById
    TextView homeFirstUser;
    @ViewById
    RelativeLayout homeFirstContainer;
    @ViewById
    GridViewForScollView homeHotGrid;
    @ViewById
    GridViewForScollView homeJingxuanGrid;
    @ViewById
    RecyclerView recyclerView;


    private Special special = new Special();

    //数据集合
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private List<HotMenu> hotMenus = new ArrayList<HotMenu>();
    private List<Special> specials = new ArrayList<Special>();
    private List<NewMenu> newmenus = new ArrayList<NewMenu>();
    private List<Work> works = new ArrayList<Work>();
    private List<NowFood> nowFoods = new ArrayList<NowFood>();
    private List<HotPeople> hotPeoples= new ArrayList<HotPeople>();
    private List<Product> products= new ArrayList<Product>();
    private List<Lab> labs= new ArrayList<Lab>();

    //适配器
    private HotMenuAdapter hotMenuAdapter;
    private SpecialAdapter specialAdapter;
    private HotPeopleAdapter hotPeopleAdapter;

    private HttpUtils utils = new HttpUtils();


    @AfterViews
    public void init(){
        initViews();
        initData();
    }

    private void initViews() {
        hotMenuAdapter  = new HotMenuAdapter(getActivity().getSupportFragmentManager(),fragments);
        homeFirstViewpager.setAdapter(hotMenuAdapter);
        homeFirstIndicator.setViewPager(homeFirstViewpager);

        specialAdapter = new SpecialAdapter(specials,getActivity());
        homeHotGrid.setAdapter(specialAdapter);

        //recycleview
        hotPeopleAdapter = new HotPeopleAdapter(hotPeoples,getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(hotPeopleAdapter);
    }

    public RequestParams initParams(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("appName", "味他");
        params.addBodyParameter("keyCode", "178ACAD8B52531AF86E6DBEDF75C3E19");
        params.addBodyParameter("insideCode", "10011");
        params.addBodyParameter("timeStamp", "1447153942150");
        params.addBodyParameter("appType", "1");
        params.addBodyParameter("ipAddress", "60.12.228.106");
        params.addBodyParameter("version", "2.1.1");

        return params;
    }

    private void initData() {

        utils.send(HttpRequest.HttpMethod.POST, AppConfig.APP_HOME, initParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.i("tag", responseInfo.result);
                BaseBean baseBean = JSONObject.parseObject(responseInfo.result,BaseBean.class);
                hotMenus.addAll(baseBean.getData().getHotMenu())  ;
                specials.addAll(baseBean.getData().getSpecial()) ;
                newmenus.addAll(baseBean.getData().getNewmenu()) ;
                works.addAll(baseBean.getData().getWork()) ;
                nowFoods.addAll(baseBean.getData().getNowFood()) ;
                hotPeoples.addAll(baseBean.getData().getHotPeople()) ;
                products.addAll(baseBean.getData().getProduct());
                labs.addAll(baseBean.getData().getLab());

                //hotmenus
                for (int i = 0; i < hotMenus.size(); i++) {
                    fragments.add(BlankFragment.newInstance(hotMenus.get(i)));
                }
                hotMenuAdapter.notifyDataSetChanged();
                //special
                special.setSpecialId("more");
                special.setSpecialPicId("more");
                special.setSpecialName("更多");
                specials.add(special);
                specialAdapter.notifyDataSetChanged();

                //hotpeople
                hotPeopleAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
